<!--#
layout("/layouts/platform.html"){
#-->
<div id="app" v-cloak>
    <el-row class="header navbar bg-white shadow">
        <div class="btn-group tool-button mt5">
            <el-date-picker
                    v-model="searchDate"
                    type="datetimerange"
                    :picker-options="pickerOptions"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    align="right"
                    :default-time="['00:00:00', '23:59:59']"
                    value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
        </div>
        <div class="btn-group tool-button mt5">
            <el-select v-model="pageForm.bizType" filterable placeholder="请选择类型">
                <el-option v-for="item in bizTypeOptions"
                           :value="item.val"
                           :label="item.name"></el-option>
            </el-select>
        </div>
        <div class="btn-group tool-button mt5">
            <el-select
                    v-model="pageForm.currencyId"
                    filterable
                    reserve-keyword
                    placeholder="选择币种"
                    :loading="loading">
                <el-option
                        v-for="item in currencies"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id">
                </el-option>
            </el-select>
        </div>
        <!--搜索用户-->
        <div class="btn-group tool-button mt5">
            <el-input v-model="pageForm.keyword" @keyup.enter.native="doSearch()" placeholder="用户昵称、用户名、手机号">
            </el-input>
        </div>
        <!--搜索按钮-->
        <div class="btn-group tool-button mt5">
            <el-button slot="append" icon="el-icon-search" @click="doSearch"></el-button>
        </div>
    </el-row>
    <el-row class="el-table-container">
        <el-table
                :data="tableData"
                @sort-change='pageOrder'
                size="small"
                header-align="center"
                style="width: 100%"
                @selection-change="handleSelectionChange"
                :default-sort="{prop: 'opAt', order: 'descending'}">
            <el-table-column
                    type="selection"
                    width="35">
            </el-table-column>

            <el-table-column label="用户名" header-align="left" prop="userName"
                             :show-overflow-tooltip="true">
            </el-table-column>

            <el-table-column label="用户昵称" header-align="left" prop="nickName"
                             :show-overflow-tooltip="true">
            </el-table-column>

            <el-table-column label="手机号" header-align="left" prop="phone"
                             :show-overflow-tooltip="true">
            </el-table-column>

            <el-table-column label="币种" header-align="left" prop="currencyName"
                             :show-overflow-tooltip="true">
            </el-table-column>

            <el-table-column label="操作金额" header-align="left" prop="amount"
                             :show-overflow-tooltip="true">
            </el-table-column>

            <el-table-column label="可用余额" header-align="left" prop="balance"
                             :show-overflow-tooltip="true">
            </el-table-column>

            <el-table-column sortable label="操作时间" header-align="center" align="center" prop="opAt">
                <template slot-scope="scope">
                    {{formatAt(scope.row.opAt)}}
                </template>
            </el-table-column>

            <el-table-column label="业务类型" header-align="left" prop="bizType"
                             :show-overflow-tooltip="true">
                <template slot-scope="scope">
                    {{getBizTypeName(scope.row.bizType)}}
                </template>
            </el-table-column>

            <el-table-column label="业务单号" header-align="left" prop="bizNo"
                             :show-overflow-tooltip="true">
            </el-table-column>
        </el-table>
    </el-row>
    <el-row class="el-pagination-container">
        <el-pagination
                @size-change="pageSizeChange"
                @current-change="pageNumberChange"
                :current-page="pageForm.pageNumber"
                :page-sizes="[10, 20, 30, 50]"
                :page-size="pageForm.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pageForm.totalCount">
        </el-pagination>
    </el-row>

</div>
<script>
    new Vue({
        el: "#app",
        data: function () {
            return {
                pickerOptions: {
                    disabledDate: function (time) {
                        return time.getTime() > new Date(new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000);
                    },
                    shortcuts: [{
                        text: '最近一周',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-1, 'weeks').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一个月',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-1, 'months').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近三个月',
                        onClick: function (picker) {
                            var end = new Date();
                            var start = new Date();
                            var temp = moment().add(-3, 'months').valueOf();
                            start.setTime(temp);
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                },
                bizTypeOptions: [],
                addDialogVisible: false,
                editDialogVisible: false,
                selectData: [],
                pageForm: {
                    searchName: "",
                    keyword: "",
                    searchDate: "",
                    pageNumber: 1,
                    pageSize: 10,
                    totalCount: 0,
                    pageOrderName: "opAt",
                    pageOrderBy: "descending",
                    bizType: ""
                },
                tableData: [],
                currencies: [],
                formData: {
                    id: "",
                },
                formRules: {
                    toUser: [
                        {required: true, message: '充值对象', trigger: 'blur'}
                    ],
                    amount: [
                        {required: true, message: '金额', trigger: 'blur'}
                    ]
                },
                searchDate: [],
                options: [],
                loading: false,
                options2: []
            }
        },
        methods: {
            pageOrder: function (column) {//按字段排序
                this.pageForm.pageOrderName = column.prop;
                this.pageForm.pageOrderBy = column.order;
                this.pageData();
            },
            pageNumberChange: function (val) {//页码更新操作
                this.pageForm.pageNumber = val;
                this.pageData();
            },
            pageSizeChange: function (val) {//分页大小更新操作
                this.pageForm.pageSize = val;
                this.pageData();
            },
            doSearch: function () {
                this.pageForm.pageNumber = 1;
                this.pageData();
            },
            pageData: function () {//加载分页数据
                var self = this;
                sublime.showLoadingbar();//显示loading
                if (self.searchDate) {
                    self.pageForm.searchDate = self.searchDate.toString();
                }
                $.post(base + "/platform/user/account/record/data", self.pageForm, function (data) {
                    sublime.closeLoadingbar();//关闭loading
                    if (data.code === 0) {
                        self.tableData = data.data.list;
                        self.pageForm.totalCount = data.data.totalCount;
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json");
            },
            formatAt: function (val) {
                if (val > 0)
                    return moment(val * 1000).format("YYYY-MM-DD HH:mm");
                return "";
            },
            getBizTypeName: function (val) {
                for (let bizTypeKey in this.bizTypeOptions) {
                    if (this.bizTypeOptions[bizTypeKey].val === val) {
                        return this.bizTypeOptions[bizTypeKey].name;
                    }
                }
            },
            handleSelectionChange: function (val) {
                this.selectData = val;
            },
            openAdd: function () {
                var self = this;
                self.addDialogVisible = true;
                self.formData = {
                    id: ""
                };//打开新增窗口,表单先清空
                if (self.$refs["addForm"])
                    self.$refs["addForm"].resetFields();
            },
            doAdd: function () {
                var self = this;
                var url = base + "/platform/user/account/recharge/add";
                self.$refs["addForm"].validate(function (valid) {
                    if (valid) {//表单验证通过
                        $.post(url, self.formData, function (data) {
                            if (data.code == 0) {
                                self.$message({
                                    message: data.msg,
                                    type: 'success'
                                });
                                self.addDialogVisible = false;
                                self.pageData();
                            } else {
                                self.$message({
                                    message: data.msg,
                                    type: 'error'
                                });
                            }
                        }, "json");
                    }
                });
            },
            doEdit: function () {
                var self = this;
                var url = base + "/platform/cms/link/class/editDo";
                self.$refs["editForm"].validate(function (valid) {
                    if (valid) {//表单验证通过
                        $.post(url, self.formData, function (data) {
                            if (data.code == 0) {
                                self.$message({
                                    message: data.msg,
                                    type: 'success'
                                });
                                self.editDialogVisible = false;
                                self.pageData();
                            } else {
                                self.$message({
                                    message: data.msg,
                                    type: 'error'
                                });
                            }
                        }, "json");
                    }
                });
            },
            delMore: function () {
                var self = this;
                if (self.selectData.length == 0) {
                    self.$message({
                        message: "请选择消息",
                        type: 'warning'
                    });
                    return false;
                }
                var ids = [];
                self.selectData.forEach(function (val) {
                    ids.push(val.id);
                });
                self.$confirm('您确定要删除选中的 ' + ids.length + ' 条数据？ ', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning',
                    callback: function (a, b) {
                        if ("confirm" == a) {//确认后再执行
                            $.post(base + "/platform/cms/link/class/delete", {ids: ids.toString()}, function (data) {
                                if (data.code == 0) {
                                    self.$message({
                                        message: data.msg,
                                        type: 'success'
                                    });
                                    self.pageData();
                                } else {
                                    self.$message({
                                        message: data.msg,
                                        type: 'error'
                                    });
                                }
                            }, "json");
                        }
                    }
                });
            },
            getBizType: function () {
                var self = this;
                $.post(base + "/platform/user/account/record/getBizType", function (data) {
                    if (data.code === 0) {
                        self.bizTypeOptions = data.data;
                        self.bizTypeOptions.unshift({
                            value: '',
                            name: '全部类型'
                        })
                    }
                }, "json");
            },
            getCurrencies: function (str) {
                var _this = this;
                $.post(base + "/platform/sys/currency/list", {name: str}, function (data) {
                    _this.currencies = data.data.map(ite => {
                        ite.name = ite.name + "(" + ite.rate + ")";
                        return ite;
                    })
                }, "json");
            }
        },
        created: function () {
            this.pageData();
            this.getBizType();
            this.getCurrencies();
        }
    });
</script>
<!--#
}
#-->